﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Forms.VisualStyles;
using System.Windows.Input;
using Microsoft.VisualStudio.RegularExpression;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using SandcastleUtils.Drop;
using SandcastleUtils.Points;

namespace SandcastleUtils
{
    /// <summary>
    /// Handles the key pressed event to launch the 
    /// </summary>
    internal class LaunchEditorKeyProcessor : KeyProcessor
    {
        private readonly static Regex NewRegexStatementForCSharpOrVisualBasic = new Regex(@"<image\s+.*xlink:href$", RegexOptions.Singleline | RegexOptions.Compiled);
        private UserControl1 infobox;
        private IAdornmentLayer _adornmentLayer;

        private SVsServiceProvider serviceProvider;

        private IWpfTextView view;
        //private RegexEditorService regexEditorService;
        private KeyTypeConverter keyTypeConverter;

        internal LaunchEditorKeyProcessor(IWpfTextView view, SVsServiceProvider _serviceProvider)
        {
            this.serviceProvider = _serviceProvider;
            this.view = view;

            this.view.ViewportHeightChanged += new EventHandler(view_ViewportHeightChanged);

            _adornmentLayer = this.view.GetAdornmentLayer("SandcastleUtils");
            _adornmentLayer.TextView.ViewportHeightChanged += new EventHandler(view_ViewportHeightChanged);


            infobox = new UserControl1(view, _adornmentLayer, serviceProvider);
           
            
            
            this.keyTypeConverter = new KeyTypeConverter();
        }

     
        void view_ViewportHeightChanged(object sender, EventArgs e)
        {
            //clear the adornment layer of previous adornments
            _adornmentLayer.RemoveAllAdornments();
        }

        public override void KeyDown(KeyEventArgs args)
        {
            char? key = this.keyTypeConverter.ConvertToChar(args.Key);
            // Check if the parethesis key was pressed
            if (key.HasValue && key.Value == '=')
            {
                // Create a regex line data
                RegexLineData regexLineData = RegexLineData.CreateFromCaretPosition(this.view);

                infobox.CaretPosition = regexLineData.CaretPosition;

                // Check if the line contains a new Regex statement
                if (IsValid(regexLineData))
                {
                    this.view.TextBuffer.Insert(regexLineData.CaretPosition, key.ToString());
                    //// Show the regex editor
                    //RegexEditorResult editorResult = this.regexEditorService.ShowEditor();
                     double top = view.Caret.Top;
                     double right = view.Caret.Right;



                     //clear the adornment layer of previous adornments
                     _adornmentLayer.RemoveAllAdornments();

                    //Place the image in the top right hand corner of the Viewport
                    Canvas.SetLeft(infobox, right );
                    Canvas.SetTop(infobox, top );
            
                    //add the image to the adornment layer and make it relative to the viewport
                    _adornmentLayer.AddAdornment(AdornmentPositioningBehavior.ViewportRelative, null, null, infobox, null);
                    

                    
                    //// Insert the edited pattern in the line
                    //if (editorResult.Result.HasValue && editorResult.Result.Value)
                    //{
                    //    this.view.TextBuffer.Insert(regexLineData.CaretPosition + 1, string.Format("@\"{0}\"", editorResult.Pattern));
                    //}

                    args.Handled = true;
                }
            }
        }

        /// <summary>
        /// Returns true if the regex line ends with a "new Regex" statement (for C# or VB)
        /// </summary>
        /// <param name="regexLineData"></param>
        /// <returns></returns>
        private bool IsValid(RegexLineData regexLineData)
        {
            return NewRegexStatementForCSharpOrVisualBasic.IsMatch(regexLineData.Text.TrimEnd());
        }
    }
}